package com.css.dzj.dzjap.modules.construnit.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.HttpClientUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.construnit.entity.ConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.service.ConstrunitService;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 建筑单位controller
 */
@RequestMapping("/construct")
@RestController
@PropertySource(value = "classpath:base.properties")
public class ConstrunitController {

    @Autowired
    private ConstrunitService construnitService;

    @Autowired
    private EnterpriseService enterpriseService;
    @Value("${SAVE_USER}")
    private String SAVE_USER;
    @Value("${BASE_URL}")
    private  String BASE_URL;

    @Autowired
    BaseService baseService;
    /**
     * 建设单位注册
     * @param construct
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody ConstructionUnitEntity construct) {
        try {

            Map<String,String> map=new HashMap();
            if(StringUtils.isEmpty(construct.getUniscid()) || StringUtils.isEmpty(construct.getEntName())
                        || StringUtils.isEmpty(construct.getLocation()) || StringUtils.isEmpty(construct.getLegalPerson())
                        || StringUtils.isEmpty(construct.getEntType()) || StringUtils.isEmpty(construct.getLinkMan())
                        || StringUtils.isEmpty(construct.getLinkTel()) || StringUtils.isEmpty(construct.getPassword())
                        || StringUtils.isEmpty(construct.getUserName())) {
                return R.error("必填字段不能为空");
            }
            // 判断该建设单位是否在黑名单
            ApEnterpriseCreditEntity tyReConEnterPrise = enterpriseService.getTyReConEnterPrise("1", construct.getUniscid());
            if(tyReConEnterPrise != null) {
                return R.error("该单位已存在于黑名单中，无法注册!");
            }
            // 判断企业名称是否已存在
            boolean isExistUserName = construnitService.isExistUserName(construct.getUserName());
            if(isExistUserName) {
                return R.error("用户名已存在!");
            }
            // 判断统一社会信用代码是否已存在
            boolean isExistEntCode = construnitService.isExistEntCode(construct.getUniscid());
            if(isExistEntCode) {
                return R.error("统一社会信用代码已存在!");
            }
            boolean flag=baseService.checkUserExist(construct.getUserName());
            if(flag) {
                return R.error("用户名已存在");
            }else {
                //String url = BASE_URL+SAVE_USER;
                if(!StringUtils.isEmpty(construct.getUserName())) {
                    String loginName = construct.getUserName();
                    map.put("loginName",loginName);
                }else {
                    return R.error("用户名不能为空");
                }

                if(!StringUtils.isEmpty(construct.getPassword())){
                    String password= construct.getPassword();
                    map.put("password",password);
                }else {
                    return R.error("密码不能为空");
                }
                String jianshe="jianshe";

           /* map.put("userSystem",jianshe);
            String msg=HttpClientUtil.doPost(url,map);*/
                Map msg= baseService.add(construct.getUserName(),jianshe,construct.getPassword());
                String message= JSON.toJSONString(msg);
                JSONObject jsonObject=JSONObject.parseObject(message);
                if("500".equals(jsonObject.get("code").toString())){
                    return R.error((String)jsonObject.get("message"));
                }
                construnitService.regist(construct);
                baseService.logSuccess(3,"单位注册成功","2");
                return R.ok();
            }

        }catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"单位注册成功","2");
            return R.error(e.getMessage());
        }
    }

}
